<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/*
 * 处理发件人/收件人信息
 */

class Import extends Base_Controller {

    public $filedata; //文件中的数据
    public $addresslist; //可用的联系人数据
    public $sameaddresslist; //重复的联系人数据
    public $group; //收件人分组

    public function __construct() {
        parent::__construct();
        // 加载excel类
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
    }

    /*
     * 处理文件中的联系人数据
     */

    public function import_send_file($table, $filesrc) {
        $PHPExcel = new PHPExcel();
        $department_id = $this->session->department_id;
        $uid = $this->session->id;
        $address = [];
        $extension = pathinfo($filesrc)['extension'];
        if ($extension == 'xlsx' || $extension == 'xls') {
            //excel文件
            if ($extension == 'xlsx') {
                $this->load->library('phpexcel/Reader/Excel2007.php');
                $PHPReader = new Excel2007();
            } else if ($extension == 'xls') {
                $this->load->library('phpexcel/Reader/PHPExcel_Reader_Excel5.php');
                $PHPReader = new PHPExcel_Reader_Excel5();
            }
            $PHPExcel = $PHPReader->load($filesrc);
            $currentSheet = $PHPExcel->getSheet(0);
            //获取总列数  
            $allColumn = $currentSheet->getHighestColumn();
            //获取总行数  
            $allRow = $currentSheet->getHighestRow();
            //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始  
            for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
                //从哪列开始，A表示第一列  
                for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                    //数据坐标  
                    $address = $currentColumn . $currentRow;
                    //读取到的数据，保存到数组$arr中  
                    $this->filedata[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
                }
            }
            foreach ($this->filedata as $k => $v) {
                $k = $k - 1;
                $email = trim($v['A']);
                $approve_username = trim($v['B']);
                $approve_password = trim($v['C']);
                $mail_host = strtolower(trim($v['D'])) == 'qq' ? 'smtp.qq.com' : 'smtp.163.com';
                $where = [
                    'email' => $email,
                    'department_id' => $department_id
                ];
                $samedata = $this->address_model->checkAddress($table, $where);
                if (empty($email)) {
                    continue;
                } else if (!empty($samedata)) {
                    $this->sameaddresslist[$k]['email'] = $email;
                    continue;
                }
                $this->addresslist[$k]['email'] = $email;
                $this->addresslist[$k]['approve_username'] = $approve_username;
                $this->addresslist[$k]['approve_password'] = $approve_password;
                $this->addresslist[$k]['mail_host'] = $mail_host;
                $this->addresslist[$k]['create_time'] = tnow();
                $this->addresslist[$k]['department_id'] = $department_id;
                $this->addresslist[$k]['uid'] = $uid;
            }
        } else if ($extension == 'txt') {
            $file = fopen($filesrc, "r");
            $i = 0;
            while (!feof($file)) {
                $list[$i] = fgets($file); //fgets()函数从文件指针中读取一行
                $i++;
            }
            fclose($file);
            $list = array_filter($list);
            foreach ($list as $k => $v) {
                $this->filedata[$k] = explode(',', trim($v));
            }
            foreach ($this->filedata as $k => $v) {
                $email = trim($v[0]);
                $approve_username = trim($v[1]);
                $approve_password = trim($v[2]);
                $mail_host = strtolower(trim($v['3'])) == 'qq' ? 'smtp.qq.com' : 'smtp.163.com';
                $where = [
                    'email' => $email,
                    'department_id' => $department_id
                ];
                $samedata = $this->address_model->checkAddress($table, $where);
                if (empty($v)) {
                    continue;
                } else if (!empty($samedata)) {
                    $this->sameaddresslist[$k]['email'] = $v;
                    continue;
                }
                $this->addresslist[$k]['email'] = $email;
                $this->addresslist[$k]['approve_username'] = $approve_username;
                $this->addresslist[$k]['approve_password'] = $approve_password;
                $this->addresslist[$k]['mail_host'] = $mail_host;
                $this->addresslist[$k]['create_time'] = tnow();
                $this->addresslist[$k]['department_id'] = $department_id;
                $this->addresslist[$k]['uid'] = $uid;
            }
        }
    }

    public function import_receive_file($table, $filesrc) {
        $department_id = $this->session->department_id;
        $uid = $this->session->id;
        $address = [];
        $extension = pathinfo($filesrc)['extension'];
        if ($extension == 'xlsx' || $extension == 'xls') {
            //excel文件
            if ($extension == 'xlsx') {
                $this->load->library('phpexcel/Reader/Excel2007.php');
                $PHPReader = new Excel2007();
            } else if ($extension == 'xls') {
                $this->load->library('phpexcel/Reader/PHPExcel_Reader_Excel5.php');
                $PHPReader = new PHPExcel_Reader_Excel5();
            }
            $PHPExcel = $PHPReader->load($filesrc);
            $currentSheet = $PHPExcel->getSheet(0);
            //获取总列数  
            $allColumn = $currentSheet->getHighestColumn();
            //获取总行数  
            $allRow = $currentSheet->getHighestRow();
            //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始  
            for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
                //从哪列开始，A表示第一列  
                for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                    //数据坐标  
                    $address = $currentColumn . $currentRow;
                    //读取到的数据，保存到数组$arr中  
                    $this->filedata[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
                }
            }
            foreach ($this->filedata as $k => $v) {
                $k = $k - 1;
                $email = trim($v['A']);
                $where = [
                    'email' => $email,
                    'department_id' => $department_id
                ];
                $samedata = $this->address_model->checkAddress($table, $where);
                if (empty($email)) {
                    continue;
                } else if (!empty($samedata)) {
                    $this->sameaddresslist[$k]['email'] = $email;
                    continue;
                }
                if (!empty($this->group)) {
                    $this->addresslist[$k]['group_id'] = $this->group;
                }
                $this->addresslist[$k]['email'] = $email;
                $this->addresslist[$k]['create_time'] = tnow();
                $this->addresslist[$k]['department_id'] = $department_id;
                $this->addresslist[$k]['uid'] = $uid;
            }
        } else if ($extension == 'txt') {
            $file = fopen($filesrc, "r");
            $i = 0;
            while (!feof($file)) {
                $list[$i] = fgets($file); //fgets()函数从文件指针中读取一行
                $i++;
            }
            fclose($file);
            $this->filedata = array_filter($list);
            foreach ($this->filedata as $k => $v) {
                $email = trim($v);
                $where = [
                    'email' => $email,
                    'department_id' => $department_id
                ];
                $samedata = $this->address_model->checkAddress($table, $where);
                if (empty($v)) {
                    continue;
                } else if (!empty($samedata)) {
                    $this->sameaddresslist[$k]['email'] = $v;
                    continue;
                }
                if (!empty($this->group)) {
                    $this->addresslist[$k]['group_id'] = $this->group;
                }
                $this->addresslist[$k]['email'] = $email;
                $this->addresslist[$k]['create_time'] = tnow();
                $this->addresslist[$k]['department_id'] = $department_id;
                $this->addresslist[$k]['uid'] = $uid;
            }
        }
    }

    /*
     * 导入文件中的联系人数据
     */

    public function import_file_data() {
        $redata = $this->httpcode;
        $type = $this->input->post('type');
        if ($type == 1) {
            $table = 'su_sender';
            $msg = '发件人';
        } else {
            $table = 'su_receiver';
            $msg = '收件人';
            $this->group = $this->input->post('group');
        }
        
//        var_dump($this->group);
//        die();
        
        $filesrc = $this->input->post('file');
        $filesrc = './uploads/address_file/' . $filesrc;
        if ($type == 1) {
            $this->import_send_file($table, $filesrc);
        } else {
            $this->import_receive_file($table, $filesrc);
        }
        if (empty($this->addresslist)) {
            $redata['errno'] = 600;
            $redata['error'] = $msg . '信息数据重复,导入失败';
            $redata['sameaddresslist'] = $this->sameaddresslist;
        } else if (!empty($this->addresslist)) {
            if ($this->address_model->insertAddress($table, $this->addresslist)) {
                $redata['errno'] = 666;
                $redata['error'] = $msg . '信息导入成功';
                $redata['addresslist'] = $this->addresslist;
                $redata['sameaddresslist'] = $this->sameaddresslist;
            } else {
                $redata['errno'] = 603;
                $redata['error'] = $msg . '信息导入失败';
            }
        } else {
            $redata['errno'] = 601;
            $redata['error'] = $msg . '信息导入失败';
        }
        echo json_encode($redata);
    }

    /*
     * 逐条录入联系人数据
     */

    public function import_data_handy() {
        $redata = $this->httpcode;
        $type = $this->input->post('type');
        if ($type == 1) {
            $table = 'su_sender';
            $msg = '发件人';
            $approve_username = $this->input->post('approve_username');
            $approve_password = $this->input->post('approve_password');
            $smtp = $this->input->post('smtp');
            trim($smtp) == 'qq' ? $mail_host = 'smtp.qq.com' : $mail_host = 'smtp.163.com';
        } else {
            $table = 'su_receiver';
            $msg = '收件人';
        }
        $department_id = $this->session->department_id;
        $uid = $this->session->id;
        $email = trim($this->input->post('email'));
        $where = [
            'email' => $email,
            'department_id' => $department_id
        ];
        $data = [
            'email' => $email,
            'department_id' => $department_id,
            'uid' => $uid,
            'create_time' => tnow()
        ];
        if ($type == 1) {
            $data['approve_username'] = trim($approve_username);
            $data['approve_password'] = trim($approve_password);
            $data['mail_host'] = $mail_host;
        } else {
            $data['group_id'] = $this->input->post('group');
        }

        $sameaddresslist = $this->address_model->checkAddress($table, $where);

        if (empty($sameaddresslist)) {
            if ($this->address_model->insertAddressHandy($table, $data)) {
                $redata['errno'] = 666;
                $redata['error'] = $msg . '信息添加成功';
            } else {
                $redata['errno'] = 601;
                $redata['error'] = $msg . '信息添加失败';
            }
        } else {
            $redata['errno'] = 600;
            $redata['error'] = $msg . '信息添加失败,数据重复';
        }
        echo json_encode($redata);
    }

}
